﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using Assist;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Electrical;
using Autodesk.Revit.UI;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls;

namespace HYRElectric.WirePlace
{
	public partial class DlgCreateConduitByWire : System.Windows.Forms.Form
	{
		public DlgCreateConduitByWire()
		{
			this.InitializeComponent();
		}

		public DlgCreateConduitByWire(Document doc)
		{
			this.InitializeComponent();
			this.m_docment = doc;
			this.GetSharePara_Value();
			this.GetFitting(5, this.cbxElbowFitting);
			this.GetConduitFamily();
			this.HideForm();
		}

		private void GetSharePara_Value()
		{
			Common.GetSharedParameters(this.cbxConduitMaterial, "Conduit", "ConduitMaterial", "Material");
			Common.GetSharedParameters(this.cbxConduitDiameter, "Conduit", "ConduitDiameter", "Diameter");
			if (this.cbxConduitMaterial.Items.Count > 0)
			{
				this.cbxConduitMaterial.SelectedIndex = 0;
			}
			if (this.cbxConduitDiameter.Items.Count > 0)
			{
				this.cbxConduitDiameter.SelectedIndex = 0;
			}
		}

		private void GetConduitFamily()
		{
			try
			{
				IList<Element> allConduitType = this.GetAllConduitType();
				if (allConduitType.Count <= 0)
				{
					MessageBox.Show("检测到项目中没有所需要的线管族，请先加载族。", "盈建科提示");
					this.ResultDlg = Result.Failed;
				}
				else
				{
					foreach (Element element in allConduitType)
					{
						Parameter parameter = element.get_Parameter(BuiltInParameter.ALL_MODEL_FAMILY_NAME);
						if (!this.cbxConduitFamily.Items.Contains(parameter.AsString()))
						{
							this.cbxConduitFamily.Items.Add(parameter.AsString());
						}
					}
					if (this.cbxConduitFamily.Items.Count > 0)
					{
						this.cbxConduitFamily.SelectedIndex = 0;
					}
					else
					{
						MessageBox.Show("检测到项目中没有所需要的线管族，请先加载族。", "盈建科提示");
						this.ResultDlg = Result.Failed;
					}
				}
			}
			catch (Exception)
			{
			}
		}

		private void cbxConduitFamily_SelectedIndexChanged(object sender, EventArgs e)
		{
			try
			{
				this.cbxConduitType.Items.Clear();
				IList<Element> allConduitType = this.GetAllConduitType();
				if (allConduitType.Count <= 0)
				{
					MessageBox.Show("当前项目中没有匹配的配件类型", "盈建科提示");
					this.ResultDlg = Result.Failed;
				}
				else
				{
					foreach (Element element in allConduitType)
					{
						if (element.get_Parameter(BuiltInParameter.ALL_MODEL_FAMILY_NAME).AsString() == this.cbxConduitFamily.Text)
						{
							this.cbxConduitType.Items.Add(element);
						}
					}
					if (this.cbxConduitType.Items.Count > 0)
					{
						this.cbxConduitType.SelectedIndex = 0;
					}
					else
					{
						MessageBox.Show("当前项目中没有匹配的配件类型", "盈建科提示");
						this.ResultDlg = Result.Failed;
					}
				}
			}
			catch (Exception)
			{
			}
		}

		private void SaveDlgData()
		{
			FileStream fileStream = new FileStream(Path.GetTempPath() + "CreateConduitByWire.dat", FileMode.Create);
			StreamWriter streamWriter = new StreamWriter(fileStream);
			string value = Convert.ToString(this.cbxConduitDiameter.SelectedIndex);
			streamWriter.WriteLine(value);
			value = Convert.ToString(this.cbxConduitMaterial.SelectedIndex);
			streamWriter.WriteLine(value);
			value = Convert.ToString(this.txtHeight.Text);
			streamWriter.WriteLine(value);
			value = Convert.ToString(this.cbxConduitFamily.SelectedIndex);
			streamWriter.WriteLine(value);
			value = Convert.ToString(this.cbxConduitType.SelectedIndex);
			streamWriter.WriteLine(value);
			value = Convert.ToString(this.cbxElbowFitting.SelectedIndex);
			streamWriter.WriteLine(value);
			value = Convert.ToString(this.cbxElbowFittingType.SelectedIndex);
			streamWriter.WriteLine(value);
			streamWriter.Flush();
			streamWriter.Close();
			fileStream.Close();
		}

		private void LoadDlgData()
		{
			string path = Path.GetTempPath() + "CreateConduitByWire.dat";
			if (!File.Exists(path))
			{
				return;
			}
			FileStream fileStream = new FileStream(path, FileMode.Open);
			StreamReader streamReader = new StreamReader(fileStream);
			streamReader.BaseStream.Seek(0L, SeekOrigin.Begin);
			string text = streamReader.ReadLine();
			this.cbxConduitDiameter.SelectedIndex = Convert.ToInt32(text);
			text = streamReader.ReadLine();
			this.cbxConduitMaterial.SelectedIndex = Convert.ToInt32(text);
			text = streamReader.ReadLine();
			this.txtHeight.Text = text;
			text = streamReader.ReadLine();
			this.cbxConduitFamily.SelectedIndex = Convert.ToInt32(text);
			text = streamReader.ReadLine();
			this.cbxConduitType.SelectedIndex = Convert.ToInt32(text);
			text = streamReader.ReadLine();
			this.cbxElbowFitting.SelectedIndex = Convert.ToInt32(text);
			text = streamReader.ReadLine();
			this.cbxElbowFittingType.SelectedIndex = Convert.ToInt32(text);
			streamReader.Close();
			fileStream.Close();
		}

		private void DlgCreateConduitByWire_Load(object sender, EventArgs e)
		{
			//base.Icon = Resource.YArchitechIcon;
			this.LoadDlgData();
		}

		private void btnExtend_Click(object sender, EventArgs e)
		{
			if (this.sel)
			{
				base.Width = 258;
				base.Height = 359;
				this.groupBox2.Visible = true;
				this.sel = false;
			}
			else
			{
				this.HideForm();
			}
			this.ClickEx = 1;
		}

		private void HideForm()
		{
			base.Width = 258;
			base.Height = 223;
			this.groupBox2.Visible = false;
			this.sel = true;
		}

		private void DlgCreateConduitByWire_FormClosed(object sender, FormClosedEventArgs e)
		{
			this.SaveDlgData();
		}

		private void txtHeight_KeyPress(object sender, KeyPressEventArgs e)
		{
			Common.IsDouble(e, this.txtHeight);
		}

		private void cbxElbowFitting_SelectedIndexChanged(object sender, EventArgs e)
		{
			this.GetFittingType(this.cbxElbowFittingType, this.cbxElbowFitting.Text);
		}

        private void GetFitting(int value, YArchitech.Electric.Controls.YJKComboBox cbx)
		{
			try
			{
				IList<Element> allConduitFitting = this.GetAllConduitFitting();
				if (allConduitFitting.Count <= 0)
				{
					MessageBox.Show("检测到项目没有所需要的配件族，请先加载族。", "盈建科提示");
					this.ResultDlg = Result.Failed;
				}
				else
				{
					foreach (Element element in allConduitFitting)
					{
						if ((element as FamilySymbol).Family.get_Parameter(BuiltInParameter.FAMILY_CONTENT_PART_TYPE).AsInteger() == value)
						{
							this.defaultValue.Add(element.Id);
							Parameter parameter = element.get_Parameter(BuiltInParameter.ALL_MODEL_FAMILY_NAME);
							if (!cbx.Items.Contains(parameter.AsString()))
							{
								cbx.Items.Add(parameter.AsString());
							}
						}
					}
					if (cbx.Items.Count > 0)
					{
						cbx.SelectedIndex = 0;
					}
					else
					{
						MessageBox.Show("检测到项目没有所需要的配件族，请先加载族。", "盈建科提示");
						this.ResultDlg = Result.Failed;
					}
				}
			}
			catch (Exception)
			{
			}
		}

        private void GetFittingType(System.Windows.Forms.ComboBox cbx, string cbxtext)
		{
			try
			{
				cbx.Items.Clear();
				IList<Element> allConduitFitting = this.GetAllConduitFitting();
				if (allConduitFitting.Count <= 0)
				{
					MessageBox.Show("当前项目中没有匹配的配件类型。", "盈建科提示");
					this.ResultDlg = Result.Failed;
				}
				else
				{
					foreach (Element element in allConduitFitting)
					{
						if (element.get_Parameter(BuiltInParameter.ALL_MODEL_FAMILY_NAME).AsString() == cbxtext)
						{
							cbx.Items.Add(element);
						}
					}
					if (cbx.Items.Count > 0)
					{
						cbx.SelectedIndex = 0;
					}
					else
					{
						MessageBox.Show("当前项目中没有匹配的配件类型。", "盈建科提示");
						this.ResultDlg = Result.Failed;
					}
				}
			}
			catch (Exception)
			{
			}
		}

		private IList<Element> GetAllConduitType()
		{
			return new FilteredElementCollector(this.m_docment).OfClass(typeof(ConduitType)).ToElements();
		}

		private IList<Element> GetAllConduitFitting()
		{
			FilteredElementCollector filteredElementCollector = new FilteredElementCollector(this.m_docment);
			filteredElementCollector.OfClass(typeof(FamilySymbol)).OfCategory(BuiltInCategory.OST_ConduitFitting);
			return filteredElementCollector.ToElements();
		}

		public Result ResultDlg
		{
			get
			{
				return this.resultDlg;
			}
			set
			{
				this.resultDlg = value;
			}
		}

		public int ClickEx
		{
			get
			{
				return this.clickEx;
			}
			set
			{
				this.clickEx = value;
			}
		}

		public List<ElementId> DefaultValue
		{
			get
			{
				return this.defaultValue;
			}
			set
			{
				this.defaultValue = value;
			}
		}

		private void btnOk_Click(object sender, EventArgs e)
		{
			if (Common.CheckValue(this.txtHeight, "相对标高"))
			{
				base.DialogResult = DialogResult.OK;
			}
		}

		private Document m_docment;

		private bool sel;

		private Result resultDlg;

		private int clickEx;

		private List<ElementId> defaultValue = new List<ElementId>();
	}
}
